package com.nf.health.app.main_intervention.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;

import com.nf.health.app.R;
import com.nf.health.app.customview.TextProgressBarView;
import com.nf.health.app.main_intervention.bean.MedicinedDietBean;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ScreentUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by he_pan on 2016/11/24.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author he_pan
 * @Description 食品类别和营养占比
 */

public class GvFoodAdapter extends BaseAdapter {
    private Context ctx;
    //膳食配比数据集合
    private List<MedicinedDietBean.FoodlistBean> mFoodList;
    //item大小,限制为屏幕的1/4
    private int mWidth;
    //本膳食卡路里总量,用于设置pb的占比
    private int mTotalCalorie;

    public GvFoodAdapter(Context ctx, MedicinedDietBean mDietBean) {
        this.ctx = ctx;
        this.mFoodList = mDietBean.getFoodlist();
        for (int i = 0; i < mFoodList.size(); i++) {
            int c = Integer.valueOf(mFoodList.get(i).getCalorie());
            mTotalCalorie += c;
        }
        mWidth = (ScreentUtils.getScreenWidth(ctx) - DensityUtil.dip2px(ctx, 35)) / 4;
    }

    @Override
    public int getCount() {
        return mFoodList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MedicinedDietBean.FoodlistBean bean = mFoodList.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(ctx, R.layout.item_gv_food, null);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(mWidth, mWidth);
            convertView.setLayoutParams(params);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tpFoodView.setCenterTitle(bean.getType());
        holder.tpFoodView.setProgress(mTotalCalorie,Integer.valueOf(bean.getCalorie()));
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tp_food_view)
        TextProgressBarView tpFoodView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
